package com.hu.eurekaproducer.controller;

import com.hu.eurekaproducer.utlis.ZipUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.zip.ZipOutputStream;

/**
 * @Author lsp
 * @Date 2019/12/28 14:16
 * @Version 1.0
 */

@RestController
public class HelloController {
    private static final Logger log = LoggerFactory.getLogger(HelloController.class);
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);
        log.info("------------------发送参数 {}",name);

        return "hello "+name+"，this is first messge";
    }


    @RequestMapping("/fForm/enclosureExport")
    public void enclosureExport(HttpServletRequest request, HttpServletResponse response) throws Exception {
        File isFile = new File("E:/ceshi");
        this.judeDirExists(isFile);
        File file1 = null;
        OutputStream out = response.getOutputStream();
        // 压缩包名称
        String downloadName = "报工.zip";
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(downloadName, "utf-8"));
        response.setContentType("application/octet-stream;charset=UTF-8");
        createZip();
        File zipFile = new File("E:/ceshi/报工.zip");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(zipFile);
            out = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[1024];
            while ((b = fis.read(buffer)) != -1) {
                // 4.写到输出流(out)中
                out.write(buffer, 0, b);
            }
            fis.close();
            out.flush();
            out.close();
            File file = new File("E:/ceshi");//输入要删除文件目录的绝对路径
            deleteFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public byte[] createZip() throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        // 将目标文件打包成zip导出
        FileOutputStream fos1 = new FileOutputStream(new File("E:/ceshi/报工.zip"));
        ZipUtils.toZip("E:/ceshi", fos1,true);
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public static void deleteFile(File file){
          int flag = 1;//用来判断文件是否删除成功
        //判断文件不为null或文件目录存在
        if (file == null || !file.exists()){
            flag = 0;
            System.out.println("文件删除失败,请检查文件路径是否正确");
            return;
        }
        //取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f: files){
            //打印文件名
            String name = file.getName();
            System.out.println(name);
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()){
                deleteFile(f);
            }else {
                f.delete();
            }
        }
        //删除空文件夹  for循环已经把上一层节点的目录清空。
        file.delete();
    }

    // 判断文件夹是否存在
    public static void judeDirExists(File file) {

        if (file.exists()) {
            if (file.isDirectory()) {
                System.out.println("dir exists");
            } else {
                System.out.println("the same name file exists, can not create dir");
            }
        } else {
            System.out.println("dir not exists, create it ...");
            file.mkdir();
        }

    }


}
